package dt.tradeAction;

import java.io.IOException;
import java.io.Reader;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.opensymphony.xwork2.ActionSupport;

import dt.bankAction.AccountHisVO;
import dt.bankAction.AccountVO;

public class TradeAction extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		
	private int endFlag;
	private String resultView;
	private int used_money;
	private double startRates2;
	private double endRates;
	private String id;
	private Map session;
	
	private int minCheck;
	
	private String page1;
	private String national;
	
	private AccountVO paramClass;
	private AccountHisVO paramClass_his;
	
	public static Reader reader;
	public static SqlMapClient sqlMapper;
	
	public TradeAction() throws IOException {
		reader = Resources.getResourceAsReader("sqlMapConfig.xml"); //sqlMapConfig.xml �좎룞�쇿뜝�숈삕�좎룞���좎룞�쇿뜝�숈삕�좎룞�쇿뜝�숈삕�좎룞���좎룞�쇿뜝�숈삕�좎듅�먯삕.
		sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader); //sqlMapConfig.xml �좎룞�쇿뜝�숈삕�좎룞���좎룞�쇿뜝�숈삕�좑옙sqlMapper �좎룞�숈껜 �좎룞��
		reader.close();
	}
	
	public String execute() throws Exception {
		int resultMoney = 0;
		paramClass = new AccountVO();
		paramClass_his = new AccountHisVO();
		endFlag = 1;

		national_select();		
		System.out.println(page1);
		System.out.println(national);
		//id = session.get("id").toString();
		id = "kimdw";
		
		String totalMoney = (String) sqlMapper.queryForObject("account.select-totalMoney", id);
		//System.out.println(totalMoney);
		
		if (resultView.equals("WIN")){
			if (minCheck == 1) {
				resultMoney = (int) (Integer.parseInt(totalMoney) + ( used_money * 1.76));
				paramClass.setId(id);
				paramClass.setTotal_money(resultMoney);
				
				sqlMapper.update("account.resultUpdateMoney", paramClass);
				
				///// history insert /////
				
				paramClass_his.setId(id);
				paramClass_his.setUsed_money(used_money);
				paramClass_his.setSuccess_money((int)(used_money * 1.76)); 
				paramClass_his.setNational(national);
				
				sqlMapper.insert("accountHis.insertHisResult", paramClass_his);
				
			} else if (minCheck == 2){
				resultMoney = (int) (Integer.parseInt(totalMoney) + ( used_money * 1.75));
				paramClass.setId(id);
				paramClass.setTotal_money(resultMoney);
				sqlMapper.update("account.resultUpdateMoney", paramClass);
				
				///// history insert /////
				
				paramClass_his.setId(id);
				paramClass_his.setUsed_money(used_money);
				paramClass_his.setSuccess_money((int)(used_money * 1.76)); 
				paramClass_his.setNational(national);
							
				sqlMapper.insert("accountHis.insertHisResult", paramClass_his);
			}
		}
		else if (resultView.equals("LOSE")){
			resultMoney = Integer.parseInt(totalMoney) - used_money;
			paramClass.setId(id);
			paramClass.setTotal_money(resultMoney);
			sqlMapper.update("account.resultUpdateMoney", paramClass);
		
			///// history insert /////
			
			paramClass_his.setId(id);
			paramClass_his.setUsed_money(used_money);
			paramClass_his.setSuccess_money(0); 
			paramClass_his.setNational(national);
									
			sqlMapper.insert("accountHis.insertHisResult", paramClass_his);			
			
		}
		return INPUT;
	}
	
	public void national_select () {
		
		/*if (page1.equals("http://localhost:8080/new_DTR/tradeFormJPY.action")){
			national = "JPY";
		}
		else if (page1.equals("http://localhost:8080/new_DTR/tradeFormUSD.action")) {
			national = "USD";
		}
		else if (page1.equals("http://localhost:8080/new_DTR/tradeFormEUR.action")) {
			national = "EUR";
		}*/
		
		
		switch (page1) {
		case "http://localhost:8080/new_DTR/tradeFormJPY.action":
			national = "JPY";
			break;
		case "http://localhost:8080/new_DTR/tradeFormUSD.action" :	
			national = "USD";
			break;
		case "http://localhost:8080/new_DTR/tradeFormEUR.action":
			national = "EUR";
			break;
		default :
			break;
		}
	}
	
	public String form() throws Exception {
		
		return SUCCESS;
	}
	
	public String getResultView() {
		return resultView;
	}
	
	public void setResultView(String resultView) {
		this.resultView = resultView;
	}
	
	
	public int getEndFlag() {
		return endFlag;
	}

	public void setEndFlag(int endFlag) {
		this.endFlag = endFlag;
	}

	public int getUsed_money() {
		return used_money;
	}

	public void setUsed_money(int used_money) {
		this.used_money = used_money;
	}

	public double getStartRates2() {
		return startRates2;
	}

	public void setStartRates2(double startRates2) {
		this.startRates2 = startRates2;
	}

	public double getEndRates() {
		return endRates;
	}

	public void setEndRates(double endRates) {
		this.endRates = endRates;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;		
	}
	public Map getSession(){
		return session;
	}

	public int getMinCheck() {
		return minCheck;
	}

	public void setMinCheck(int minCheck) {
		this.minCheck = minCheck;
	}
	
	public String getPage1() {
		return page1;
	}

	public void setPage1(String page1) {
		this.page1 = page1;
	}

}
